package com.his.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.entity.Medicinal;
import com.his.mapper.MedicinalMapper;
import com.his.service.MedicinalService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Medicinal)表服务实现类
 *
 * @author makejava
 * @since 2020-10-24 11:11:27
 */
@Service("medicinalService")
public class MedicinalServiceImpl implements MedicinalService {
    @Resource
    private MedicinalMapper medicinalMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Medicinal queryById(Integer id) {
        return this.medicinalMapper.queryById(id);
    }

    /**
     * 查询所有通过实体类属性
     *
     * @param medicinal 实例对象
     * @return 查询的结果
     */
    @Override
    public List<Medicinal> queryAll(Medicinal medicinal) {
        return this.medicinalMapper.queryAll(medicinal);
    }

    public PageInfo<Medicinal> queryAll(PageParam<Medicinal> pageParam) {

        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for (int i = 0; i < pageParam.getOrderParams().length; i++) {
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            return new PageInfo<Medicinal>(medicinalMapper.superSearch(pageParam.getSuperSearchKeyWord()));
        }
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        //排序
        for (int i = 0; i < pageParam.getOrderParams().length; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        return new PageInfo<Medicinal>(medicinalMapper.queryAllByModel(pageParam.getModel()));
    }

    /**
     * 新增数据
     *
     * @param medicinal 实例对象
     * @return 实例对象
     */
    @Override
    public Medicinal insert(Medicinal medicinal) {
        medicinal.setCreateTime(new Date());
        medicinal.setIsCheck(1);
        medicinal.setIsDelete(0);
        medicinal.setIsShelf(0);
        //判断类别名称是否重复
        Integer queryByName = medicinalMapper.queryByName(medicinal.getMedicinalName());
        if (queryByName > 0) {
            return null;
        }
        //没重复则返回添加的实体
        this.medicinalMapper.insert(medicinal);
        return medicinal;
    }

    /**
     * 修改数据
     *
     * @param medicinal 实例对象
     * @return 实例对象
     */
    @Override
    public Medicinal update(Medicinal medicinal) {
        //判断类别名称是否重复
//        Integer queryByName = medicinalMapper.queryByName(medicinal.getMedicinalName());
//        if (queryByName > 0) {
//            return null;
//        }
        this.medicinalMapper.update(medicinal);
        return this.queryById(medicinal.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.medicinalMapper.deleteById(id) > 0;
    }

    /**
     * 通过主键逻辑删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteByIdLogic(Integer id) {
        return this.medicinalMapper.deleteByIdLogic(id) > 0;
    }

    /**
     * 上架药品
     * @param medicinal
     * @return
     */
    @Override
    public Integer shelfMedicinal(Medicinal medicinal) {
        if (medicinal.getIsShelf()==0){
            medicinal.setIsShelf(1);
        }else{
            medicinal.setIsShelf(0);
        }
        int update = medicinalMapper.update(medicinal);
        return update;
    }

    /**
     * 根据类别查找药品
     * @param medicinal
     * @return
     */
    @Override
    public Rsp selectMedicinalBySuperId(Integer id) {
        List<Medicinal> medicinalList=medicinalMapper.selectMedicinalBySuperId(id);
        if (StringUtils.isEmpty(medicinalList)){
            return Rsp.fail().msg("当前类别没有商品");
        }else{
            return Rsp.success(medicinalList);
        }

    }
}
